package com.slt.android.customview.d2canvas;


import android.content.Context;
import android.content.res.Resources;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Paint;
import android.support.annotation.Nullable;
import android.util.AttributeSet;
import android.util.DisplayMetrics;
import android.view.View;

/**
 * Created by dp on 2017/12/25.
 */

public class MeterView extends View {

    int screenWidth, screenHeight;
    Paint p;

    int percent;
    int minute;
    int hour;

    Paint pHour , pMinute;


    public MeterView(Context context) {
        super(context);
        init(context);
    }

    public MeterView(Context context, @Nullable AttributeSet attrs) {
        super(context, attrs);
        init(context);
    }

    public MeterView(Context context, @Nullable AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
        init(context);
    }

    private void init(Context context) {
        Resources resources = this.getResources();
        DisplayMetrics dm = resources.getDisplayMetrics();
        screenWidth = dm.widthPixels;
        screenHeight = dm.heightPixels;

        p = new Paint();
        p.setColor(Color.BLUE);
        p.setStyle(Paint.Style.STROKE);
        p.setStrokeWidth(5);
        p.setAntiAlias(true);





        pMinute = new Paint();
        pMinute.setColor(Color.GREEN);
        pMinute.setStyle(Paint.Style.STROKE);
        pMinute.setStrokeWidth(3);
        pMinute.setAntiAlias(true);

        pHour = new Paint();
        pHour.setColor(Color.BLACK);
        pHour.setStyle(Paint.Style.STROKE);
        pHour.setStrokeWidth(3);
        pHour.setAntiAlias(true);
    }


    @Override
    protected void onDraw(Canvas canvas) {
        super.onDraw(canvas);
        drawCircle(canvas);
        drawDegree(canvas);
        drawLine(canvas);
    }

    private void drawLine(Canvas canvas) {
        percent += 6;
        if(percent>=360){
            percent =0;
            minute+=1;
        }
        canvas.save();
        canvas.rotate(percent,screenWidth / 2, screenHeight / 2);
        canvas.drawLine(screenWidth/2 , screenHeight/2 , screenWidth/2 , (screenHeight - screenWidth) / 2 + 200 , p);
        canvas.restore();
        if(minute > 60){
            minute=0;
            hour+=1;
        }
        canvas.save();
        canvas.rotate(6 * minute,screenWidth / 2, screenHeight / 2);
        canvas.drawLine(screenWidth/2 , screenHeight/2 , screenWidth/2 , (screenHeight - screenWidth) / 2 + 300 , pMinute);
        canvas.restore();

        canvas.save();
        canvas.rotate(6 * hour,screenWidth / 2, screenHeight / 2);
        canvas.drawLine(screenWidth/2 , screenHeight/2 , screenWidth/2 , (screenHeight - screenWidth) / 2 + 480 , pHour);
        canvas.restore();

        if(hour > 60){
            hour=0;
        }
        postInvalidateDelayed(5);

    }

    private void drawDegree(Canvas canvas) {
        p.setStrokeWidth(3);
        for (int i = 0; i < 60; i++) {
            if (i == 0 || i == 5 || i == 10 || i == 15 || i == 20 ||
                    i == 25 || i == 30 || i == 35 || i == 40 || i == 45 ||
                    i == 50 || i == 55) {
                p.setStrokeWidth(3);
                p.setTextSize(30);
                canvas.drawLine(screenWidth / 2, (screenHeight - screenWidth) / 2, screenWidth / 2, (screenHeight - screenWidth) / 2 + 40, p);
                String degree = String.valueOf(i);
                canvas.drawText(degree, screenWidth / 2 - p.measureText(degree) / 2,
                        (screenHeight - screenWidth) / 2 + 60, p);
            } else {
                p.setStrokeWidth(2);
                p.setTextSize(20);
                canvas.drawLine(screenWidth / 2, (screenHeight - screenWidth) / 2, screenWidth / 2, (screenHeight - screenWidth) / 2 + 15, p);

                String degree = String.valueOf(i);
                canvas.drawText(degree, screenWidth / 2 - p.measureText(degree) / 2,
                        (screenHeight - screenWidth) / 2 + 30, p);
            }
            canvas.rotate(6, screenWidth / 2, screenHeight / 2);
        }
    }

    private void drawCircle(Canvas canvas) {
        canvas.drawCircle(screenWidth / 2, screenHeight / 2, screenWidth / 2, p);
    }
}
